﻿using System;
using System.Data;
using System.Text;
using System.Data.OracleClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class UDesktop : UserPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoginUserTip.Text = MenuString();
            Head1.Title = "欢迎登录" + ConfigurationManager.AppSettings["SystemName"];
            Sysword.InnerText = ConfigurationManager.AppSettings["SystemName"];
        }
    }

    protected void LBlogout_Click(object sender, EventArgs e)
    {
        if (Session["LoginUser"] != null)
        {
            log4net.LogManager.GetLogger(GetType()).Info("{ClientIP:'" + Tools.GetClientIP() + "',Msg:'"
                + Session["LoginUser"].ToString() + " logout.'}");
        }
        Session["LoginUser"] = null;
        Response.Redirect("~/ULogin.aspx");
    }

    private string MenuString()
    {
        StringBuilder sb = new StringBuilder("[");
        string sqlStr = @"SELECT DISTINCT Data_Code,Data_Name FROM U_PUB_DATA_T T1
                                    WHERE DATA_TYPE = 'SysModule' AND IF_VALID = 'Y' 
                                   AND EXISTS(SELECT * FROM U_MENU_T T2 WHERE T2.Menu_Type = T1.Data_Code 
                                                       AND T2.Menu_Status = '是' AND IF_MOBILE = 'N'
                                                       AND EXISTS(SELECT * FROM u_Role_Page_t T3 
                                                       WHERE T3.PAGE_CODE = T2.Menu_Title AND T3.IF_VALID='Y'
                                                       AND EXISTS(SELECT * FROM u_User_Role_t T4
                                                       WHERE T4.Role_Code = T3.ROLE_CODE AND T4.Login_Name=:login_name
                                                       AND T4.IF_VALID = 'Y')))
                                 ORDER BY ORDER_NUM";
        OracleParameter[] _params = {
            new OracleParameter(":login_name",OracleType.VarChar,16)
        };
        _params[0].Value = LoginUser;
        DataTable dt = OracleHelper.ExecuteDataSet(sqlStr, CommandType.Text, _params).Tables[0];
        RenderORRedirect(dt);
        foreach (DataRow dr in dt.Rows)
        {
            sb.Append("{");
            sb.Append("title:'" + dr["Data_Name"].ToString() + "'");
            sb.Append(MenuTitle(dr["Data_Code"].ToString()));
            sb.Append("},");
        }
        if (sb.Length > 1)
        {
            sb.Remove(sb.Length - 1, 1);
        }
        sb.Append("]");
        return Server.HtmlEncode(sb.ToString());
    }

    private string MenuTitle(string module)
    {
        string sqlStr = @"SELECT DISTINCT T4.MENU_URL AS page_url,T4.MENU_TITLE AS page_title,T4.MENU_NUMBER AS order_num
                                      FROM U_USER_ROLE_T T1 
                                     INNER JOIN (SELECT data_code,data_name FROM U_PUB_DATA_T
                                                  WHERE data_type = 'role_list' AND if_valid = 'Y') T2
                                        ON T1.ROLE_CODE = T2.DATA_CODE
                                     INNER JOIN U_ROLE_PAGE_T T3
                                        ON T1.ROLE_CODE = T3.ROLE_CODE
                                       AND T3.IF_VALID = 'Y'
                                     INNER JOIN U_MENU_T T4
                                        ON T3.PAGE_CODE = T4.MENU_TITLE
                                       AND T4.MENU_STATUS = '是'
                                       AND T4.MENU_TYPE = :menu_type
                                       AND T4.IF_MOBILE = 'N'
                                     WHERE T1.Login_Name = :login_name
                                       AND T1.If_Valid = 'Y'
                                     ORDER BY T4.MENU_NUMBER";
        OracleParameter[] _params = {
            new OracleParameter(":login_name",OracleType.VarChar,16),
            new OracleParameter(":menu_type",OracleType.VarChar,20)
        };
        _params[0].Value = LoginUser;
        _params[1].Value = module;
        DataTable dt = OracleHelper.ExecuteDataSet(sqlStr, CommandType.Text, _params).Tables[0];
        StringBuilder sb = new StringBuilder(",html:'<ul>");
        foreach (DataRow dr in dt.Rows)
        {
            sb.Append("<li><a class="+"menubar"+" href=");
            sb.Append(dr["page_url"].ToString());
            sb.Append(" target=UFrame>");
            sb.Append(dr["page_title"].ToString());
            sb.Append("</a></li>");
        }
        sb.Append("</ul>'");
        return sb.ToString();
    }    

    private void RenderORRedirect(DataTable argDt)
    {
        if (argDt.Rows.Count == 0)
        {
            UFrame.InnerText = "用户权限配置有误，请退出系统并联系系统管理员！";
            log4net.LogManager.GetLogger(GetType()).Error("PC端桌面用户" + Session["LoginUser"].ToString() + "未配置权限！");
        }
        else
        {
            string sqlStr = @"SELECT DISTINCT T4.MENU_URL AS page_url,T4.MENU_TITLE AS page_title,T4.MENU_NUMBER AS order_num
                                      FROM U_USER_ROLE_T T1 
                                     INNER JOIN (SELECT data_code,data_name FROM U_PUB_DATA_T
                                                  WHERE data_type = 'role_list' AND if_valid = 'Y') T2
                                        ON T1.ROLE_CODE = T2.DATA_CODE
                                     INNER JOIN U_ROLE_PAGE_T T3
                                        ON T1.ROLE_CODE = T3.ROLE_CODE
                                       AND T3.IF_VALID = 'Y'
                                     INNER JOIN U_MENU_T T4
                                        ON T3.PAGE_CODE = T4.MENU_TITLE
                                       AND T4.MENU_STATUS = '是'
                                       AND T4.MENU_TYPE = :menu_type
                                     WHERE T1.Login_Name = :login_name
                                       AND T1.If_Valid = 'Y'
                                     ORDER BY T4.MENU_NUMBER";
            OracleParameter[] _params = {
                new OracleParameter(":login_name",OracleType.VarChar,16),
                new OracleParameter(":menu_type",OracleType.VarChar,20)
            };
            _params[0].Value = LoginUser;
            _params[1].Value = argDt.Rows[0]["Data_Code"].ToString();
            DataTable dt = OracleHelper.ExecuteDataSet(sqlStr, CommandType.Text, _params).Tables[0];
            UFrame.Attributes.Add("src", dt.Rows[0]["page_url"].ToString());
        }
    }
}
